
const Article = require('../model/article');
const Tag = require('../model/tag');
const User = require('../model/user');
const { myHash, jwtSign } = require('../utils/index');
class apiController {
    static async createdTag(ctx) {
        let { title, user_id } = ctx.request.body;
        let token = ctx.request.token;
        if (!token) {
            ctx.body = {
                msg: '登录过期',
                code: 401,
            }
            return;
        }
        try {
            await Tag.create({
                user_id,
                title,
                createtime: new Date(),
                updatetime: new Date()
            })
            ctx.body = {
                msg: '创建成功',
                code: 200
            }
        } catch (error) {
            ctx.body = {
                msg: '网络错误',
                code: 500,
            }
        }
    }
    static async getTags(ctx) {
        const {user_id} = ctx.request.query
        try {
            let res = await Tag.findAll({
                // attributes: ['id', 'title', 'createtime', 'updatetime'],
                order: [
                    ['updatetime', 'DESC']
                ],
                where:{
                    user_id
                }
            })
            ctx.body = {
                msg: '获取成功',
                code: 200,
                data: res
            }
        } catch (error) {
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }
    static async getTag(ctx) {
        let { id } = ctx.request.query;
        try {
            const res = await Tag.findOne({
                where: {
                    id
                }
            })
            ctx.body = {
                msg: '获取成功',
                code: 200,
                data: res
            }
        } catch (error) {
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }
    static async createArticle(ctx) {

        let { user_id, title, tag_id, content } = ctx.request.body;
        let token = ctx.request.token;
        if (!token) {
            ctx.body = {
                msg: '登录过期',
                code: 401,
            }
            return;
        }
        try {
            await Article.create({
                user_id,
                title,
                tag_id,
                content,
                createtime: new Date(),
                updatetime: new Date()
            })
            ctx.body = {
                msg: '创建成功',
                code: 200
            }
        } catch (error) {
            console.log('error', error);
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }

    }

    static async getArticles(ctx) {
        let { tag_id, user_id } = ctx.request.query;
        let order = [
            ['createtime', 'DESC']
        ]
        try {
            let res = []
            if (tag_id != null) {
                res = await Article.findAll({
                    attributes: ['id', 'title', 'user_id', 'tag_id', 'content', 'is_active', 'createtime', 'updatetime'],
                    where: {
                        tag_id: tag_id,
                        user_id
                    },
                    order
                });
            } else {
                res = await Article.findAll({
                    attributes: ['id', 'title', 'user_id', 'tag_id', 'content', 'is_active', 'createtime', 'updatetime'],
                    order,
                    where: {
                        user_id
                    }
                });
            }
            ctx.body = {
                msg: '获取成功',
                code: 200,
                data: res
            }
        } catch (error) {
            console.log('error', error);
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }
    static async getArticle(ctx) {
        let { id } = ctx.request.query;
        try {
            const res = await Article.findOne({
                where: {
                    id
                }
            })
            ctx.body = {
                msg: '获取成功',
                code: 200,
                data: res
            }
        } catch (error) {
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }
    static async login(ctx) {
        let { username, password } = ctx.request.body;
        try {
            let user = await User.findOne({
                where: {
                    username
                }
            })
            if (user.length != 0) {

                if (password != user.password) {
                    ctx.body = {
                        msg: '密码不匹配',
                        code: 201
                    }
                    return;
                }
                const token = await jwtSign({ id: user.id });
                ctx.body = {
                    msg: '登录成功',
                    code: 200,
                    data: {
                        user: {
                            id: user.id,
                            username: user.username,
                            token
                        }
                    }
                }
            } else {
                ctx.body = {
                    msg: '未找到该用户',
                    code: 201
                }
            }

        } catch (error) {
            console.log(error, 'error');
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }

    static async createTag(ctx) {

        let { user_id, title } = ctx.request.body;
        try {
            await Tag.create({
                user_id,
                title,
                createtime: new Date(),
                updatetime: new Date()
            })
            ctx.body = {
                msg: '创建成功',
                code: 200
            }
        } catch (error) {
            console.log('error', error);
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }

    }

    static async deleteTag(ctx) {

        let { tag_id } = ctx.request.body;
        try {
            const res = await Tag.destroy({
                where: {
                    id: tag_id
                }
            });
            ctx.body = {
                msg: '删除成功',
                code: 200
            }
        } catch (error) {
            console.log('error', error);
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }

    }
    static async changeArticle(ctx) {
        let { id, is_active } = ctx.request.body;
        try {
            const res = await Article.update({
                is_active: !is_active?0:1
            },
                {
                    where: {
                        id
                    }
                });
            ctx.body = {
                msg: '操作成功',
                code: 200
            }
        } catch (error) {
            console.log('error', error);
            ctx.body = {
                msg: '网络错误',
                code: 500
            }
        }
    }
}

module.exports = apiController